//
//  MainTabBarController.swift
//  SwiftWB
//
//  Created by 远成物流 on 2017/6/22.
//
//

import Foundation
import UIKit

class MainTabbarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setTabbar()
    }
    
    func setTabbar() {
        
        ///设置发布按钮
        let composeBtn = UIButton()
        composeBtn.backgroundColor = UIColor.orange
        composeBtn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        composeBtn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
        composeBtn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
        composeBtn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: .highlighted)
        composeBtn.sizeToFit()
        tabBar.addSubview(composeBtn)
        
        composeBtn.center = CGPoint(x: tabBar.center.x, y: tabBar.bounds.size.height*0.5)
        /*
        ///获取文件路径
        guard let tabInfoPath = Bundle.main.path(forResource: "TabInfo.json", ofType: nil) else{
            Log("获取json路径失败")
            return
        }
        
        let url = URL(fileURLWithPath: tabInfoPath)
        guard let tabInfoData = try? Data(contentsOf: url, options: .dataReadingMapped) else{
            Log("获取jsondata失败")
            return
        }
        
        ///将data转换成json
        guard let tabInfoJson = try? JSONSerialization.jsonObject(with: tabInfoData, options: .mutableContainers) else{
            Log("data转json失败")
            return
        }
        
        let tabInfoArray = tabInfoJson as! [[String:AnyObject]]
        
        ///
        for dict in tabInfoArray {
            
            guard let className = dict["className"] as? String else {
                continue
            }
            
            guard let title = dict["title"] as? String else {
                continue
            }
            
            guard let icon = dict["icon"] as? String else {
                continue
            }
            
            addChildViewController(className, title: title, icon: icon)
        }
         */
    }
    
    func addChildViewController(_ childVC: String, title:String, icon:String) {
        
        ///根据info.plist 获取命名空间
        guard let projectName = Bundle.main.infoDictionary!["CFBundleName"] as? String else{
            Log("未获取到命名空间")
            return
        }
        
        ///通过命名空间和类名转换成类
        guard let childClass = NSClassFromString(projectName + "." + childVC) else{
            Log("类名转换失败")
            return
        }
        
        ///通过Class创建一个对象,必须告诉系统Class的类型
        guard let childVCType = childClass as? UIViewController.Type else{
            Log("class创建对象失败")
            return
        }
        
        ///创建controller
        let controller = childVCType.init()
        
        ///添加导航栏
        let childNav = UINavigationController(rootViewController: controller)
        
        ///设置controller
        controller.title = title
        controller.tabBarItem.title = title
        controller.tabBarItem.image = UIImage(named: icon)
        controller.tabBarItem.selectedImage = UIImage(named: icon + "_highlighted")
        
        ///添加子控制器
        addChildViewController(childNav)
    }
}
